﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Compiler;

namespace WindowsWorkflow.SequentialActivities
{
	public class FollowerOfAttachedPropertyValidator:Validator 
	{
		public override ValidationErrorCollection Validate(ValidationManager manager, object obj)
		{
			ValidationErrorCollection errors = base.Validate(manager, obj);
			string activityName = obj as string;
			if(string.IsNullOrEmpty(activityName))
			{
				errors.Add(new ValidationError("FollowOf has null or empty value specified",201));
				return errors;
			}

			Activity activity = manager.Context[typeof (Activity)] as Activity;
			Pick pick = activity.Parent as Pick;
			if(pick==null)
			{
				errors.Add(new ValidationError("FollowerOf must be applied to a child activity of Pick",202));
				return errors;
			}

			Activity target = pick.Activities[activityName];
			if(target==null)
			{
				errors.Add(new ValidationError("FollowerOf must be the name of a child activity of Pick",203));
				return errors;
			}

			if(target.Name.Equals(activity.Name))
			{
				errors.Add(new ValidationError("FollowerOf cannot refer to the same activity to which it is attached",204));
				return errors;
			}

			return errors;
		}
	}
}
